package top.malegod.controller;

import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.malegod.model.User;
import top.malegod.service.UserService;
import top.malegod.utils.ServerResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/10 15:54
 * @description:  用户的基本功能
 */
@Controller
@RequestMapping("/user")
public class UserController {


  @Autowired
  private UserService userService;

  /**
   *  登录
   * @param username
   * @param password
   * @return
   */
  @RequestMapping(value = "/login.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse login(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "password") String password,
      HttpSession session){
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()){
      session.setAttribute("user",response.getData());
    }
    return response;
  }

  /**
   *  注册
   * @param user
   * @return
   */
  @RequestMapping(value = "/register.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse register(User user) throws JsonProcessingException {
        ServerResponse<User> response = userService.register(user);
        return response;
  }


  /**
   *  检查用户是否有效
   * @param username
   * @return
   */
  @RequestMapping(value = "/check_valid.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse checkValid( String username){
    ServerResponse response = userService.checkValid(username);
    return response;
  }

  /**
   *  获取登录用户信息
   * @param session
   * @return
   */
  @RequestMapping(value = "/get_user_info.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse getUserInfo(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("你还没登录，请先登录");
    }
    ServerResponse response = userService.getUserInfo(user);
    return response;
  }

  /**
   * 忘记密码
   * @param phone
   * @return
   */
  @RequestMapping(value = "/forget_pwd.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse forgetPwd(String phone) throws JsonProcessingException {
    ServerResponse response = userService.forgetPwd(phone);
    return response;
  }

  /**
   *  忘记密码的重置密码
   * @param username
   * @param passwordNew
   * @param code
   * @return
   */
  @RequestMapping(value = "/forget_reset_password.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse forgetResetPassword(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "passwordNew") String passwordNew,
      @RequestParam(value = "phone") String phone,
      @RequestParam(value = "code") String code
  ){
    ServerResponse response = userService.forgetResetPassword(username, phone, passwordNew, code);
    return response;
  }


  /**
   *  登录状态重置密码
   * @param passwordOld
   * @param passwordNew
   * @return
   */
  @RequestMapping(value = "reset_password.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse resetPassword(
      @RequestParam(value = "passwordOld") String passwordOld,
      @RequestParam(value = "passwordNew") String passwordNew,
      HttpSession session
  ){
    User user = (User) session.getAttribute("user");
    ServerResponse response = userService.resetPassword(user.getId(), passwordOld, passwordNew);
        if (response.success()){
          session.removeAttribute("user");
        }
        return response;
  }

  /**
   *  登录状态更新个人信息
   * @param user
   * @return
   */
  @RequestMapping(value = "/update_information.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse updateInformation(User user,HttpSession session){
    User user1 = (User) session.getAttribute("user");
    user.setId(user1.getId());
    ServerResponse response = userService.updateInformation(user);
        return response;
  }

  /**
   *  获取当前登录用户的详情信息，并强制登录
   * @param session
   * @return
   */
  @RequestMapping(value = "/get_information.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse getInformation(HttpSession session){
    User user = (User) session.getAttribute("user");
    ServerResponse response = userService.getInformation(user);
    if (!response.success()){
      response.setStatus(10);
    }
    session.setAttribute("user",response.getData());
    return response;
  }

  /**
   *  退出登录
   * @param session
   * @return
   */
  @RequestMapping(value = "/loginOut.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse loginOut(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("你还没登录，请登录");
    }
    session.removeAttribute("user");

    user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createSuccess("退出成功");
    }
    return ServerResponse.createError("服务端异常");
  }
}
